Conditions | 1 |
Paths | 96 |
Total Lines | 174 |
Code Lines | 132 |
Lines | 0 |
Ratio | 0 % |
Changes | 5 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | var TableHelperRequestSort = function(rq, strDesc){ |
||
191 | function initInstance(){ |
||
192 | // Singleton |
||
193 | // Private methods and variables |
||
194 | function BuildRequest(rq, crntTableId){ |
||
195 | rq.tableId = crntTableId; |
||
196 | TableHelperRequestSort(rq, instance.strDesc); |
||
197 | TableHelperRequestFilter(rq); |
||
198 | } |
||
199 | var tail = []; |
||
200 | function LoadData(tableContainer, rq){ |
||
201 | SetVisability(tableContainer, false); |
||
202 | if(window.XMLHttpRequest){ |
||
203 | var xmlhttp = new XMLHttpRequest();/* code for IE7+, Firefox, Chrome, Opera, Safari */ |
||
204 | }else{ |
||
205 | xmlhttp = new window.ActiveXObject("Microsoft.XMLHTTP");/*code for IE6, IE5 */ |
||
206 | } |
||
207 | for(var i = 0; i < tail.length; i++){ |
||
208 | var ex_xmlhttp = tail.shift(); |
||
209 | ex_xmlhttp.abort(); |
||
210 | } |
||
211 | xmlhttp.onreadystatechange = function(){ |
||
212 | if(xmlhttp.readyState === 4 && xmlhttp.status === 200){ |
||
213 | var d = JSON.parse(xmlhttp.responseText); |
||
214 | instance.rq = rq; |
||
215 | instance.Draw(tableContainer, d); |
||
216 | } |
||
217 | }; |
||
218 | xmlhttp.open("GET", RequestToUrl(rq), true); |
||
219 | xmlhttp.send(); |
||
220 | tail.push(xmlhttp); //put at tail to can abort later any previous |
||
221 | } |
||
222 | function SetTheTableColumnsHoverEffect(tContainer, tableId){ |
||
223 | var tHcells = tContainer.rows[0].cells; |
||
224 | for(var i = 0; i < tHcells.length; i++){ |
||
225 | if(tHcells[i].firstChild.tagName === "A"){ |
||
226 | tHcells[i].firstChild.setAttribute("onmouseover", "table.ColumnHover('" + tableId + "'," + i + ");"); |
||
227 | tHcells[i].firstChild.setAttribute("onmouseout", "table.ColumnHover('" + tableId + "');"); |
||
228 | } |
||
229 | } |
||
230 | } |
||
231 | function Init(tableId){ |
||
232 | var tContainer = document.getElementById(tableId); |
||
233 | if(iePrior(9)){ |
||
234 | SetTheTableColumnsHoverEffect(tContainer, tableId); |
||
235 | } |
||
236 | var tfoot = tContainer.getElementsByTagName("tfoot")[0]; |
||
237 | ProcessPaginationLinks(tfoot); |
||
238 | } |
||
239 | |||
240 | function DrawSection(tableContainer, dt, tSection){ |
||
241 | var section = tSection === "tfoot" ? "tfoot" : "tbody"; |
||
242 | var tSec = document.getElementById(tableContainer) |
||
243 | .getElementsByTagName(section)[0]; |
||
244 | ClearSection(tSec, iePrior(9)); |
||
245 | for(var i = 0; i < dt.length; i++){ |
||
246 | var row = dt[i]; |
||
247 | var tRow = document.createElement("tr"); |
||
248 | |||
249 | DrawRow(row, tRow); |
||
250 | |||
251 | tSec.appendChild(tRow); |
||
252 | if(section === "tfoot"){ |
||
253 | ProcessPaginationLinks(tSec); |
||
254 | } |
||
255 | instance.AppendRowCalback(tableContainer); |
||
256 | } |
||
257 | } |
||
258 | |||
259 | var ClearSection = TableHelperClearSection; |
||
260 | var DrawRow = TableHelperDrawRow; |
||
261 | var FilterGetTableId = TableHelperFilterGetTableId; |
||
262 | var getParent = TableHelperGetParent; |
||
263 | var iePrior = TableHelperIfPrior; |
||
264 | var ProcessPaginationLinks = TableHelperProcessPaginationLinks; |
||
265 | var RequestToUrl = TableHelperRequestToUrl; |
||
266 | var SetVisability = TableHelperSetVisability; |
||
267 | |||
268 | return { |
||
269 | rq: null, |
||
270 | strAsc: String.fromCharCode(9650), //▲ |
||
271 | strDesc: String.fromCharCode(9660),//▼ |
||
272 | ReloadData: function(tableId){ |
||
273 | var request = {}; |
||
274 | BuildRequest(request, tableId); |
||
275 | LoadData(tableId, request); |
||
276 | }, |
||
277 | Filter: function(field){ |
||
278 | var crntTableId = FilterGetTableId(field); |
||
279 | if(crntTableId !== null){ |
||
280 | var request = {}; |
||
281 | var exRq = this.rq; |
||
282 | BuildRequest(request, crntTableId); |
||
283 | if(exRq === null || |
||
284 | request.filter !== exRq.filter || |
||
285 | request.filterBy !== exRq.filterBy |
||
286 | ){ |
||
287 | LoadData(crntTableId, request); |
||
288 | } |
||
289 | } |
||
290 | }, |
||
291 | GoPage: function(lnk){ |
||
292 | var request = {}; |
||
293 | var table = getParent(lnk, "table"); |
||
294 | var crntTableId = table.getAttribute("id"); |
||
295 | BuildRequest(request, crntTableId); |
||
296 | //check & serve pagination jump links |
||
297 | var jumpDir = lnk.innerHTML.trim().substr(0, 1); |
||
298 | if(jumpDir === "+" || jumpDir === "-"){ |
||
299 | var current = table.querySelector("tfoot .paging .a").innerHTML; |
||
300 | var jump = lnk.innerHTML.replace("K", "000").replace("M", "000000000"); |
||
301 | var jumpPage = (parseInt(current) + parseInt(jump)); |
||
302 | lnk.parentNode.setAttribute("data-page", jumpPage); |
||
303 | lnk.style.transform = "none"; |
||
304 | } |
||
305 | request.pageNo = lnk.parentNode.hasAttribute("data-page") ? |
||
306 | lnk.parentNode.getAttribute("data-page") : |
||
307 | lnk.innerHTML; |
||
308 | LoadData(crntTableId, request); |
||
309 | return false; |
||
310 | }, |
||
311 | Export: function(lnk, eType){ |
||
312 | var request = {}; |
||
313 | var crntTableId = getParent(lnk, "table").getAttribute("id"); |
||
314 | BuildRequest(request, crntTableId); |
||
315 | request.exportType = ["CSV", "Excel"].indexOf(eType) >= 0 ? |
||
316 | eType : |
||
317 | "csv"; |
||
318 | window.open(RequestToUrl(request)); |
||
319 | return false; |
||
320 | }, |
||
321 | Sort: function(colNo, lnk){ |
||
322 | var request = {}; |
||
323 | var crntTableId = getParent(lnk, "table").getAttribute("id"); |
||
324 | BuildRequest(request, crntTableId); |
||
325 | if(Math.round(colNo) === request.colNo){ |
||
326 | request.colOrd = (request.colOrd === "asc" ? "desc" : "asc"); |
||
327 | }else{ |
||
328 | request.colNo = Math.round(colNo); |
||
329 | request.colOrd = "asc"; |
||
330 | } |
||
331 | LoadData(crntTableId, request); |
||
332 | /* Clear and add new sort arrow */ |
||
333 | var headSpans = getParent(lnk, "thead").getElementsByTagName("span"); |
||
334 | var length = headSpans.length; |
||
335 | for(var i = 0; i < length; i++){ |
||
336 | headSpans[i].innerHTML = ""; |
||
337 | } |
||
338 | lnk.getElementsByTagName("span")[0].innerHTML = (request.colOrd === "desc" ? this.strDesc : this.strAsc); |
||
339 | }, |
||
340 | |||
341 | ColumnHover: function(tableContainer, index){ |
||
342 | if(!iePrior(9)){ |
||
343 | TableHelperColumnHover.call(this, tableContainer, index); |
||
344 | } |
||
345 | }, |
||
346 | Draw: function(tableContainer, d){ |
||
347 | DrawSection(tableContainer, d.body); |
||
348 | DrawSection(tableContainer, d.footer, "tfoot"); |
||
349 | this.LoadEndCalback(tableContainer); |
||
350 | SetVisability(tableContainer, true); |
||
351 | if(this.rq !== null){ |
||
352 | var hover = document.getElementById(this.rq.tableId) |
||
353 | .getElementsByTagName("th")[this.rq.colNo].lang; |
||
354 | if(hover){ |
||
355 | this.ColumnHover(tableContainer, this.rq.colNo); |
||
356 | } |
||
357 | } |
||
358 | |||
359 | }, |
||
360 | init: Init, |
||
361 | LoadEndCalback: function(){},/*Allows override: function(tableId){if(tableId){...}}*/ |
||
362 | AppendRowCalback: function(){}/*Allows override: function(tableId){if(tableId){...}}*/ |
||
363 | }; |
||
364 | } |
||
365 | return { |
||
376 |
When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically: